package router

import (
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
	"web-model/handler"
)

type Router struct {
	g *gin.Engine
}

func NewRouter() *Router {
	return &Router{
		g: gin.Default(),
	}
}

//注册请求地址
func (r *Router) register() {
	r.g.GET("/login", handler.UserLogin)
	r.g.GET("/hello", handler.Hellowrold)
}

func (r *Router) Run(listenAddr string) {
	//处理跨域问题
	r.g.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"*"},
		AllowHeaders:     []string{"*"},
		AllowMethods:     []string{http.MethodGet, http.MethodPost, http.MethodHead, http.MethodOptions},
		AllowCredentials: true,
		MaxAge:           12 * time.Hour,
	}))
	r.register()
	r.g.Run(listenAddr)
}
